program TestGraph;
uses crt, graph;
type TMenuAttributes = record
 {515 bytes}
    x, y: integer;
    quantum, Focus, indent: byte;
    labels: array[1..20] of string;
    color, hColor, bgColor, brdColor, hbgColor, hbrdColor: byte;
    widthMod, heightMod: byte; {5 - 100}
  end;

type TInputAttributes = record
    x, y: integer;
    quantum, Focus, indent: byte;
    labels: array[1..10] of string;
    data: array[1..10] of string;
	maxsymb: array [1..10] of string;
    color, hColor, bgColor, brdColor, hbgColor, hbrdColor: byte;
    widthMod, heightMod: byte; {5 - 100}
  end;

type Ttext = array [0..100] of string;

type TCoordinates = record
    RelY, RelX: integer;
  end;

type TDimensions = record
    width, height: integer;
  end;

var
  MainMenuConf: TMenuAttributes;
  MainInputConf: TInputAttributes;
  code: string;
  i: byte;

procedure Matrix;
var driver, mode: integer;
begin
  driver := DETECT;
  Initgraph(driver, mode, 'C:\BGI');
end;

procedure DrawLableBox(Tlabel: string; x, y, width, height, color, bgcolor, bcolor: integer);
begin
  setcolor(bcolor);
  Rectangle(x, y, x + width, y + height);
  SetFillStyle(1, bgcolor);
  floodfill(x + 3, y + 3, bcolor);
  SetColor(color);
  OutTextXY(x + (width - TextWidth(Tlabel)) div 2, y + (height - TextHeight(Tlabel)) div 2, Tlabel);
  SetColor(bcolor);
  Rectangle(x, y, x + width, y + height);
end;

procedure DrawMenu(Config: TMenuAttributes);
var
  cords: TCoordinates;
  dims: TDimensions;
  x, y, i, width, height: integer;
  MaxHeight, MaxWidth, buffColor, buffBgcolor, buffBordColor: integer;
begin
  with Config do begin
    MaxHeight := GetMaxY - Y;
    MaxWidth := GetMaxX - X;
    dims.width := Round(MaxWidth * WidthMod * 0.01);
    dims.height := Round(quantum * MaxHeight * HeightMod * 0.01) + indent * (quantum - 1);
    cords.RelX := (MaxWidth - dims.width) div 2 + X;
    cords.RelY := (MaxHeight - dims.height) div 2 + y;
    for i := 1 to quantum do begin
      if i = Focus then begin
        buffcolor := hcolor;
        buffbgcolor := hbgcolor;
        buffBordColor := hbrdColor;
      end
      else begin
        buffcolor := color;
        buffbgcolor := bgcolor;
        buffBordColor := brdColor;
      end;

      DrawLableBox(labels[i], Round(MaxWidth - MaxWidth * WidthMod * 0.01) div 2,
        cords.RelY + Round((i - 1) * MaxHeight * HeightMod * 0.01) + (i - 1) * indent,
        Round(widthMod * MaxWidth * 0.01), Round(heightMod * MaxHeight * 0.01), buffcolor,
        buffbgcolor, buffBordColor);
    end;
  end;
end;

procedure DrawInputBox(Tlabel, Tdata: string; x, y, width, height, color, bgcolor, bcolor: integer);
var InputX: integer;
begin
  SetFillStyle(1, bgcolor);
  Bar(x, y, x + width, y + height);
  SetColor(color);
  OutTextXY(x + 5, y + (height - TextHeight(Tlabel)) div 2, Tlabel);
  InputX := x + 5 + TextWidth(Tlabel);
  SetFillStyle(1, bcolor);
  Bar(InputX, y + 5, InputX + width - 10 - TextWidth(Tlabel), y + height - 5);
  OutTextXY(InputX + 5, y + (height - TextHeight(Tdata)) div 2, Tdata);
  SetColor(bcolor);
  Rectangle(x, y, x + width, y + height);
end;

procedure DrawLogo(pageName: string);
var text: string; MaxHeight, MaxWidth, Tx, Ty: integer;
begin
  MaxHeight := GetMaxY;
  MaxWidth := GetMaxX;
  setcolor(green);
  text := 'SelfMadeDb v0.01 Beta';
  Tx := (MaxWidth - TextWidth(text)) div 2;
  Ty := Round(MaxHeight * 0.10);
  OutTextXY(Tx, Ty, text);
  OutTextXy((MaxWidth - TextWidth(pageName))div 2, Ty + TextHeight(text) + 5, pageName);
end;
procedure DrawInputBoxes(Config: TInputAttributes);
var
  cords: TCoordinates;
  dims: TDimensions;
  x, y, i, width, height: integer;
  MaxHeight, MaxWidth, buffColor, buffBgcolor, buffBordColor: integer;
begin
  with Config do begin
    MaxHeight := GetMaxY - Y;
    MaxWidth := GetMaxX - X;
    dims.width := Round(MaxWidth * WidthMod * 0.01);
    dims.height := Round(quantum * MaxHeight * HeightMod * 0.01) + indent * (quantum - 1);
    cords.RelX := (MaxWidth - dims.width) div 2 + X;
    cords.RelY := (MaxHeight - dims.height) div 2 + y;
    for i := 1 to quantum do begin
      if i = Focus then begin
        buffcolor := hcolor;
        buffbgcolor := hbgcolor;
        buffBordColor := hbrdColor;
        if data[i][Length(data[i])] <> '_' then Insert('_', data[i], Length(data[i]) + 1);
      end
      else begin
        if data[i][Length(data[i])] = '_' then Delete(data[i], Length(data[i]), 1);
        buffcolor := color;
        buffbgcolor := bgcolor;
        buffBordColor := brdColor;
      end;

      DrawInputBox(labels[i], data[i], Round(MaxWidth - MaxWidth * WidthMod * 0.01) div 2,
        cords.RelY + Round((i - 1) * MaxHeight * HeightMod * 0.01) + (i - 1) * indent,
        Round(widthMod * MaxWidth * 0.01), Round(heightMod * MaxHeight * 0.01), buffcolor, buffbgcolor, buffBordColor);
    end;
  end;
end;

procedure InputMenuLogic(var Config: TInputAttributes; var code: string);
var
  keyboard: char;
  online: boolean;
  Tfocus: string;
  keycorrect: boolean;
  symbols: string;
begin
  symbols := ' zxcvbnm,./asdfghjkl;\qwertyuiop[]ZXCVBNMASDFGHJKLQWERTYUIOP{}:<>?|1234567890';
  online := true;
  DrawInputBoxes(Config);
  while online do begin
    DrawLogo('Input Menu. BBODUTE a-Z, NAVIGATSIA STRELKAMI, ESC - OTMENA, ENTER - PODTVERDIT ');

    code := 'none';
    with Config do begin
      if KeyPressed then begin
        keycorrect := false;
        keyboard := ReadKey;
        keycorrect := Pos(keyboard, symbols) <> 0;
        if keycorrect then
          data[Focus] := data[Focus] + keyboard
        else begin
          if ord(keyboard) = 0 then keyboard := ReadKey;
          case keyboard of
            #13: code := 'accept';
            #27: code := 'cancel';
            #72: if focus <> 1 then dec(focus) else focus := quantum;
            #80: if focus <> quantum then inc(focus) else focus := 1;
            #8: Delete(data[Focus], Length(data[Focus]), 1);
          end;
        end;
        DrawInputBoxes(Config);
      end;
    end;
    Str(Config.focus, Tfocus);
    SetColor(0);
    OutTextXY(0, 0, code + TFocus + keyboard);
    if code <> 'none' then online := false;
  end;
end;

{procedure DrawText (}

{procedure MainUiCycle (var fpath: string; var fpassword: string; head: header; var}



begin


  with MainMenuConf do begin
    x := 0; y := 150;
    quantum := 4;
    labels[1] := 'TEST';
    labels[2] := '2';
    labels[3] := '3';
    labels[4] := '4';
    color := 0;
    hcolor := 1;
    bgcolor := 15;
    hbgcolor := 11;
    brdColor := 11;
    hbrdcolor := 15;
    widthMod := 30;
    Focus := 2;
    heightmod := 7;
    indent := 5;
  end;

  with MainInputConf do begin
    x := 0; y := 150;
    quantum := 4;
    labels[1] := 'Name:';
    labels[2] := 'Date:';
    labels[3] := 'Test:';
    labels[4] := 'Test:';
    data[1] := '';
    data[2] := '';
    data[3] := '';
    data[4] := '';
    color := 0;
    hcolor := 1;
    bgcolor := 15;
    hbgcolor := 11;
    brdColor := 11;
    hbrdcolor := 15;
    widthMod := 30;
    Focus := 1;
    heightmod := 7;
    indent := 5;
  end;

  Matrix;
  SetColor(15);
  Rectangle(1, 1, GetMaxX, GetMaxY); SetFillStyle(1, 8);
  floodfill(2, 2, 15);
  InputMenuLogic(MainInputConf, code);
  CloseGraph;
  ClrScr;
  WriteLn(code);
  with MainInputConf do begin
    for i := 1 to Quantum do
      WriteLn(labels[i] + ' ' + data[i]);
  end;

  ReadKey;
end.
